<?php
namespace app\api\controller;
use app\common\controller\BaseApi;

class Test extends  BaseApi
{
	//smartgym.com/api/Test/tt
	public function tt(){
		$list=db("Temp")->select();
		$i=0;
		foreach ($list as $key => $value) {
			$card_no=$value['card_no'];
			db("Interface")->where("card_no='$card_no' and shop_id=94")->update(array("end_date"=>$value['end_date']));
			db("GymOrder")->where("card_no='$card_no' and shop_id=94")->update(array("end_date"=>$value['end_date']));
			$i++;
		}
		var_dump($i);
	}
	 public function _initialize()
    {
	}
	/**
	 * 同步安科合同 
	 * smartgym.com/api/test/synccard
	 */
	function synccard(){
		$list=db("TestCard")->limit(4000,1000)->select();
		$shop_id=0;
		$i=0;
		foreach ($list as $key => $value) {
			$result=action("api/OcmApi/GetContractInfo",[$value['cardcode'],$shop_id,"0"]);
			if($result['flag']){
				$i++;
				$data=$result['data'];
				$member=array(
								"start_date"=>$data["StartDate"],
								"end_date"=>$data["EndDate"],
								"mobile"=>$data["Mobile"],
								"contract_id"=>$data["ContractID"],
								"member_id"=>$data["MemberID"],
								"name"=>$data["MemberName"],
								"surplus_num"=>$data["SurplusNum"],
								"membership_type"=>$data["MembershipType"],
								"contract_status"=>$data["ContractStatus"],
				);
				db("Interface")->where("shop_id=$shop_id and card_no='".$value['cardcode']."'")->update($member);
			}
		}
		var_dump("成功更新：".$i);
	}
		/*smartgym.com/api/Test/import/*/
		function  import(){
			$list=db("test1")->limit(5000,1000)->select();
			$i=array();
			$shop_id=0;
			foreach ($list as $key => $datas) {
					$member=array(
									"start_date"=>$datas["StartDate"],
									"end_date"=>$datas["EndDate"],
									"mobile"=>$datas["Mobile"],
									"contract_id"=>$datas["ContractID"],
									"member_id"=>$datas["MemberID"],
									"name"=>$datas["Name"],
									"surplus_num"=>0,
									"membership_type"=>2,
									"contract_status"=>1,
									"shop_id"=>$shop_id,
									"in_varifycation_type"=>0,
									"in_varifycation"=>0,
									"card_no"=>$datas['CardCode'],
				  );
				    $count=db("Interface")->where("card_no='".$datas['CardCode']."'")->count(1);
				    if($count>0){
				    	$i[]=$datas['CardCode'];
						continue;
				    }
					db("Interface")->insert($member);
			}
		var_dump($i);die;
	}
	/**
	 * 指纹长度不足200 的 设置前台验证
	 * http://smartgym.com/api/test/setFreefinger
	 */
//	function setFreefinger(){
//	 $ids=db("interface")->where(" shop_id = 70 AND LENGTH(fingerprint) < 200 ")->column("id");
//	 $ids=implode(",", $ids);
//	 db("interface")->execute("update m_interface set in_varifycation = 0,in_varifycation_type = 0  where id in($ids)");
//	 var_dump("success");
//	} 
	/**
	 * 安科v1 转 安科 v2
	 * http://smartgym.com/api/test/ak1toakv2
	 */
	function  ak1toakv2(){
		$list=db("tc")->where("LENGTH(fingerprint) >100")->limit(0,200)->column("card_no1,fingerprint");
		$card_nos=array_keys($list);
		$error="";
		foreach ($card_nos as $key => $value) {
			$shop_id=0;
			$mobile="0";
			$result=action("api/OcmApi/GetContractInfo",[$value,$shop_id,$mobile]);
			if($result['flag']){
				$datas=$result['data'];
				$member=array(
								"start_date"=>$datas["StartDate"],
								"end_date"=>$datas["EndDate"],
								"mobile"=>$datas["Mobile"],
								"contract_id"=>$datas["ContractID"],
								"member_id"=>$datas["MemberID"],
								"name"=>$datas["MemberName"],
								"surplus_num"=>$datas["SurplusNum"],
								"membership_type"=>$datas["MembershipType"],
								"contract_status"=>$datas["ContractStatus"],
								"shop_id"=>$shop_id,
								"in_varifycation_type"=>1,
								"in_varifycation"=>1,
								"card_no"=>$value,
								"fingerprint"=>$datas['fingerprint'],
			  );
				db("Interface")->insert($member);
		}else{
			$error[]=$value;
			}
		}
		var_dump($error);die;
	}
	//http://smartgym.com/api/test/fingertest
//	function fingertest(){
//		$list=db("tc")->where("LENGTH(fingerprint) >100")->limit(2400,400)->column("card_no1,fingerprint");
//		$i=0;
//		foreach ($list as $key => $value) {
//			db("Interface")->where("card_no='$key' and shop_id=63")->setField(array("fingerprint"=>$value));
//			$i++;
//		}
//		var_dump($i);die;
//	}
	/**
	 * 安科v1 转 安科 v2 （员工）
	 * http://smartgym.com/api/test/ak1toakv2_employee
	 */
	function  ak1toakv2_employee(){
		$list=db("tc")->where("length(card_no1)>8")->limit(0,100)->column("card_no1,mobile,name,fingerprint");
		$card_nos=array_keys($list);
		$error="";
		$shop_id=0;
		foreach ($list as $key => $value) {
				$member=array(
								"mobile"=>$value["mobile"],
								"name"=>$value["name"],
								"surplus_num"=>0,
								"shop_id"=>$shop_id,
								"in_varifycation_type"=>0,
								"in_varifycation"=>0,
								"card_no"=>$value['card_no1'],
								"owner_type"=>2,
								"is_original"=>1,
			  );
			  if(strlen($value['fingerprint'])>500){
			  	$member['in_varifycation_type']=1;
			  	$member['in_varifycation']=1;
				$member['fingerprint']=$value['fingerprint'];
			  }
				db("Interface")->insert($member);
		}
		var_dump($error);die;
	}
	/**
	 * 同步会员合同
	 * smartgym.com/api/test/sync_memer
	 * */
	function sync_memer(){		
		$shop_id=0;
		$cards=db("Interface")->where("shop_id=$shop_id and owner_type=1")->limit(0,1)->column("id,card_no");
		$i=0;
		foreach ($cards as $key => $value) {
			$result=action("api/OcmApi/GetContractInfo",[$value,$shop_id,"0"]);
			if($result['flag']){
				$i++;
				$data=$result['data'];
				$member=array(
					   			"id"=>$key,
								"start_date"=>$data["StartDate"],
								"end_date"=>$data["EndDate"],
								"mobile"=>$data["Mobile"],
								"contract_id"=>$data["ContractID"],
								"member_id"=>$data["MemberID"],
								"name"=>$data["MemberName"],
								"surplus_num"=>$data["SurplusNum"],
								"membership_type"=>$data["MembershipType"],
								"contract_status"=>$data["ContractStatus"],
				);
				db("Interface")->update($member);
			}
		}
		var_dump("成功更新：".$i);
	}
/**
 * 返回true
 * https://smartgymdemo.ocmcom.com/api/test/returnture
 * */	
function returnture(){
	header('Content-Type:application/json; charset=utf-8');
    exit("true");die;
}
/**
 * 返回false
 * http://smartgymdemo.ocmcom.com/api/Test/returnfalse
 * */	
function returnfalse(){
	header('Content-Type:application/json; charset=utf-8');
    exit("false");die;
}
/*
 * 清理表数
 * */
function clearTable(){
	//$shop_id=61;
//	db("Interface")->where("shop_id=$shop_id")->delete();
//	db("BehaviorLog")->where("shop_id=$shop_id")->delete();
//	db("BehaviorLogError")->where("shop_id=$shop_id")->delete();
//	db("GymCard")->where("shop_id=$shop_id")->delete();
//	db("GymEmployee")->where("shop_id=$shop_id")->delete();
//	db("GymFollowUp")->where("shop_id=$shop_id")->delete();
//	db("GymMember")->where("shop_id=$shop_id")->delete();
//	db("GymOrder")->where("shop_id=$shop_id")->delete();
//	db("GymOrderHistory")->where("shop_id=$shop_id")->delete();
//	db("GymPtFollow")->where("shop_id=$shop_id")->delete();
//	db("GymPtOrder")->where("shop_id=$shop_id")->delete();
//	db("GymPtOrderHistory")->where("shop_id=$shop_id")->delete();
	var_dump("success");
}
	
//	function test(){
//		$list=db("Interface")->field("id,card_no,fingerprint")->where("shop_id=14 ")->select();
//		$list1=db("Tc")->column("card_no1,fingerprint");
//		$card_nos=array_keys($list1);
//		foreach ($list as $key => $value) {
//			if(in_array($value['card_no'],$card_nos)){
//			$value['fingerprint']=$list1[$value['card_no']];
//			$value['in_varifycation']=1;
//			$value['in_varifycation_type']=1;
//			db("Interface")->update($value);
//			}
//		}
//		var_dump("成功");die;
//	}
//	function test(){
//		$list=db("tc")->field("id,card_no")->select();
//		foreach ($list as $key => $value) {
//			$value['card_no1']=$this->to_dec_revers($value['card_no']);
//			db("tc")->update($value);
//		}
//		var_dump("成功");
//	}
//	 function to_dec_revers($value){
//	  $bit1=substr($value, 0,2);
//	  $bit2=substr($value, 2,2);
//	  $bit3=substr($value, 4,2);
//	  $bit4=substr($value, 6,2);
//	  $dec=hexdec($bit4.$bit3.$bit2.$bit1);		
//		   return	str_pad($dec, 10,'0',STR_PAD_LEFT);		
//	 }
	
	/**
	 *smartgym.com/api/test/import_member
	 * 导入第三方系统会员
	 * 1：data/import_tmpl/test_member.csv 导入到   test_member 表中
	 * 2:去掉重复数据
	 * DELETE from m_test_member where mobile in(SELECT mobile from m_gym_member where shop_id=$shop_id)
	 * 3:依次办卡
	 * */
	function import_member(){
		//$shop_id=123;
		//$member_shop_id=45;
		//$list=db("TestMember")->limit(0,500)->select();
		$i=0;
		foreach ($list as $key => $value) {
				$value['come_from']='陌生来访';
				$value['member_ship_id']=$member_shop_id;
				$value['shop_id']=$shop_id;
				$value['web_open_up']=1;
				db("GymMember")->insert($value);
				$i++;
		}
		var_dump("success".$i);
		
	}
	/**
	 * 下载图片
	 * smartgym.com/api/test/downloadimg
	 * */
	function downloadimg(){
		$cards=[
			'0131194263',  '0177828802',  '0665491066' , '0172590706',  '0121414087',
			'0174311634',  '0173256914',  '0172639106',  '0549154520',  '0224440725',
			'0181939778',  '0181422530',  '0170829442',  '0225695621',  '0182528130',
			'0183476530',  '0362566308',  '0161431730',  '0170392258',  '0664332346',
			'0223273861',  '0155506162',  '0177925810',  '0616229653',  '0151285378',
			'0162374546',  '0230299589',  '0417611664',  '0604130485',  '0174388690',
			'0126355911',  '0125569975',  '0417432208',  '0171546546',  '0123848631',
			'0447847812',  '0617379493',  '0607843077',  '0172617122',  '0174651970',
			'0606200693',  '0175374018',  '0544917176',  '0176484562',  '0727044667',
			'0408128144',  '0617776357',  '0466788356',  '0663938218',  '0633448197',
		];
		foreach ($cards as $k => $v) {
			$list=db("BehaviorLog")->where("shop_id=137 and card_no ='$v'")->field("card_no,face_url")->order("id desc")->limit(10)->select();
		    $i=0;
			foreach ($list as $key => $value) {
				$i=$i+1;
				$this->download($value['face_url'],$value['card_no'],$i);
			}
		}
		var_dump("成功");
	}

	function test(){
		$arr=[1,2,3,4,5,6,7];
		foreach ($arr as $key => $value) {			
			unset($arr[5]);
			unset($arr[6]);
			print_r("==============");
			if(isset($arr[$key])){
				var_dump($arr[$key]);	
			}
			
		}
	}
	/**
	 * 删除包含 2个逗号的和三个逗号的  ,或者长度小于100
	 * smartgym.com/api/Test/douhao
	 * */
	function douhao(){
	    $list=db("FlatVarifyRecord")->where(" create_time>'2018-07-26 00:00:01' and create_time<'2018-07-26 23:59:59' and LENGTH(image_url)-LENGTH(REPLACE(image_url,',',''))>1 or image_url like 'null%' or image_url like ',%' or length(image_url)<100")->select();
		$i=0;
		foreach ($list as $key => $value) {
			db("FlatVarifyRecordCopy")->insert($value);
			db("FlatVarifyRecord")->delete($value['id']);
			$i+1;
		}	
		var_dump("更新数据包：".$i);
	}
	/**
	 * smartgym.com/api/Test/del
	 * */
//	function del(){
//		$list=db("FlatVarifyRecord")->limit(0,30000)->where("id in(60886)")->order("shop_id desc ,id desc")->select();
//		foreach ($list as $key => $value) {
//				db("FlatVarifyRecordCopy")->insert($value);
//				db("FlatVarifyRecord")->delete($value['id']);
//				echo $key;
//		}		
//	}
	/**
	 * smartgym.com/api/Test/delMember
	 * */
	function delMember(){
		$list=db("FlatVarifyRecord")->where("shop_id=101 and create_time>'2018-07-24 00:00:01'")->limit(0,10000)->order("shop_id desc ,id desc")->select();
		$i=0;
		foreach ($list as $key => $value) {
			if(!isset($list[$key])){
				continue;
			}
			$arr=explode(",",$value['image_url']);
			if(strpos($arr[0],".jpg")==false){
						continue;
			}
			if(strpos($arr[1],".jpg")==false){
						continue;
			}
		if(isset($list[$key+1])){
			$next=$list[$key+1];
			$arr1=explode(',',$next['image_url']);
			if(strpos($arr1[0],".jpg")==false){
						continue;
			}
			if(strpos($arr1[1],".jpg")==false){
						continue;
			}
			if($arr[1]==$arr1[1]){
					$i=$i+1;
					unset($list[$key]);
					db("FlatVarifyRecordCopy")->insert($value);
					db("FlatVarifyRecord")->delete($value['id']);
			}
		}
	}
		var_dump("删除数据：".$i);die;
}
	/**
	 * smartgym.com/api/Test/delStaff
	 * 删除相同的 人脸平板图片  教练
	 * */
	function delStaff(){
		$list=db("FlatVarifyRecord")->limit(0,10000)->where("shop_id=101 and create_time>'2018-07-24 00:00:01'")->order("shop_id desc ,id desc")->select();
		$i=0;
		foreach ($list as $key => $value) {
			if(!isset($list[$key])){
				continue;
			}
			$arr=explode(",",$value['image_url']);
			if(strpos($arr[0],".jpg")==false){
						continue;
			}
			if(strpos($arr[1],".jpg")==false){
						continue;
			}
		if(isset($list[$key+1])){
			$next=$list[$key+1];
			if($next['pt_card_no']==$value['pt_card_no']){
				$arr1=explode(',',$next['image_url']);
				if(strpos($arr1[0],".jpg")==FALSE){
					
						continue;
				}
				if(strpos($arr1[1],".jpg")==FALSE){
							continue;
				}
				if($arr[0]==$arr1[0]){
					$i=$i+1;
					unset($list[$key]);
					db("FlatVarifyRecordCopy")->insert($next);
					db("FlatVarifyRecord")->delete($next['id']);
				}
			}	
		}
	}
		var_dump("删除数据：".$i);die;
}


	function download($url,$card_no,$i)
	{
	  $img=file_get_contents($url);
	  $path = './data/test/'.$card_no;
	  is_dir($path) OR mkdir($path, 0777, true);
	  file_put_contents($path."/$i.jpg",$img);
	}
	//smartgym.com/api/test/getFile
//	function getFile(){
//		$list=db("FaceCode")->field("card_no,face1")->limit(0,200)->order("id desc")->where("shop_id=45")->select();
//		foreach ($list as $key => $value) {
//			$myfile=fopen("./data/".$value['card_no'].".txt",'wra+');
//			fwrite($myfile, $value['face1']);
//			fclose($myfile);
//		}
//		var_dump(count(1));die;
//	}
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
